<?php
// import item base
Magnum::import('utility/stack/item');
/**
 * css stacks
 * @uses Magnum_Utility_Stack_Item
 */
class Magnum_Head_Js extends Magnum_Utility_Stack_Item
{
    // only change to constructor: name
    public function __construct()
    {
        parent::__construct('__Js');

    }// end __construct


    /**
     * add js
     * @param str $js the js
     * @param str $type the type
     * @return $this
     */
    public function set($js, $type = 'OVERWRITE')
    {
        // add to stack
        return parent::_set($this->_getMarkup($js), null, $type);

    }// end add


    /**
     * append js
     * @param str $js the js
     * @param str $key the key to set the js at, default null
     * @return $this
     */
    public function append($js, $key = null)
    {
        return parent::_append($this->_getMarkup($js), $key);

    }// end append


    /**
     * prepend js
     * @param str $js the js
     * @param str $key the key to set the js at, default null
     * @return $this
     */
    public function prepend($js, $key = null)
    {
        return parent::_prepend($this->_getMarkup($js), $key);

    }// end prepend

    /**
     * get js markup
     */
    public function _getMarkup($js)
    {
        // smart check...
        // file extension must be .js
        if (substr($js, (strlen($js) - 3) !== '.js')) {
            $item = '<script type="text/javascript"><!--' . EOL . $js . EOL . '--></script>';
        } else {
            $item = '<script type="text/javascript" src="' . $js . '"></script>';
        }

        return $item;

    }// end _getMarkup

}// end Magnum_Head_Js
?>